package com.test1.administrator.website;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.test1.administrator.website.dao.DBOperator;
import com.test1.administrator.website.entity.Website;
import com.test1.administrator.website.util.websiteIsNullException;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/11.
 */

public class WebSiteEdit extends Activity {
    private DBOperator dbOperator;
    String id;

    /**
     * Called when the activity is starting.  This is where most initialization
     * should go: calling {@link #setContentView(int)} to inflate the
     * activity's UI, using {@link #findViewById} to programmatically interact
     * with widgets in the UI, calling
     * {@link #managedQuery(Uri, String[], String, String[], String)} to retrieve
     * cursors for data being displayed, etc.
     * <p>
     * <p>You can call {@link #finish} from within this function, in
     * which case onDestroy() will be immediately called without any of the rest
     * of the activity lifecycle ({@link #onStart}, {@link #onResume},
     * {@link #onPause}, etc) executing.
     * <p>
     * <p><em>Derived classes must call through to the super class's
     * implementation of this method.  If they do not, an exception will be
     * thrown.</em></p>
     *
     * @param savedInstanceState If the activity is being re-initialized after
     *                           previously being shut down then this Bundle contains the data it most
     *                           recently supplied in {@link #onSaveInstanceState}.  <b><i>Note: Otherwise it is null.</i></b>
     * @see #onStart
     * @see #onSaveInstanceState
     * @see #onRestoreInstanceState
     * @see #onPostCreate
     */
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.websiteedit);
        EditText name = (EditText) findViewById(R.id.addname);
        EditText website = (EditText) findViewById(R.id.addwebsite);
        Button cancel = (Button) findViewById(R.id.cancel);
        Button ok = (Button) findViewById(R.id.ok);

        dbOperator=new DBOperator(this);
        int position=getIntent().getIntExtra("position",-1);
        if(position!=-1) {
            List<Map<String, Website>> list = dbOperator.query();
            Map<String, Website> m = list.get(position);
            id = m.keySet().iterator().next();
            Website w = m.get(id);

            name.setText(w.getName());
            website.setText(w.getSite());

        }


    }

    /**
     * Called after {@link #onCreate} &mdash; or after {@link #onRestart} when
     * the activity had been stopped, but is now again being displayed to the
     * user.  It will be followed by {@link #onResume}.
     * <p>
     * <p><em>Derived classes must call through to the super class's
     * implementation of this method.  If they do not, an exception will be
     * thrown.</em></p>
     *
     * @see #onCreate
     * @see #onStop
     * @see #onResume
     */
    @Override
    protected void onStart() {
        super.onStart();

    }

    /**
     * Called after {@link #onRestoreInstanceState}, {@link #onRestart}, or
     * {@link #onPause}, for your activity to start interacting with the user.
     * This is a good place to begin animations, open exclusive-access devices
     * (such as the camera), etc.
     * <p>
     * <p>Keep in mind that onResume is not the best indicator that your activity
     * is visible to the user; a system window such as the keyguard may be in
     * front.  Use {@link #onWindowFocusChanged} to know for certain that your
     * activity is visible to the user (for example, to resume a game).
     * <p>
     * <p><em>Derived classes must call through to the super class's
     * implementation of this method.  If they do not, an exception will be
     * thrown.</em></p>
     *
     * @see #onRestoreInstanceState
     * @see #onRestart
     * @see #onPostResume
     * @see #onPause
     */
    @Override
    protected void onResume() {
        super.onResume();


    }

    public void finishadd(View view){
        this.finish();
    }

    public void add(View view){
        EditText name = (EditText) findViewById(R.id.addname);
        EditText website = (EditText) findViewById(R.id.addwebsite);
        String add_name = name.getText().toString();
        String add_website = website.getText().toString();


        if(!getIntent().hasExtra("position")) {
            dbOperator = new DBOperator(this);
            dbOperator.insert(add_name, add_website);
        }


        if(getIntent().hasExtra("position")){

            try {
               Website w = new Website(add_name,add_website);
                dbOperator.update(id, w);
            } catch (websiteIsNullException e) {
                e.printStackTrace();
            }
        }

        setResult(1);
        this.finish();


    }
}

